package com.fesc.trade.demo.request;

import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.fesc.trade.demo.model.BaseModel;
import com.fesc.trade.demo.util.AssertUtil;

/**
 * 退款订单查询请求
 * @author prd-zzq
 * @version $Id: MerchantCreateRequest.java, v 0.1 2016年8月18日 下午10:20:30 prd-zzq Exp $
 */
public class RefundQueryRequest extends BaseRequest {

    /**  */
    private static final long   serialVersionUID = -7249621169638265050L;

    private BizContent          bizContent       = new BizContent();

    private Map<String, String> paraMap          = new TreeMap<String, String>();

    @Override
    public void validate() {

        super.validate();

        AssertUtil.assertNotBlank(bizContent.merchantId, "merchantId should not be NULL!");

        AssertUtil.assertNotBlank(bizContent.outTradeNo + bizContent.tradeNo, "outTradeNo and tradeNo should not be NULL at the same time!");

        AssertUtil.assertNotBlank(bizContent.outRequestNo, "outRequestNo should not be NULL!");

    }

    public String getMerchantId() {
        return this.bizContent.merchantId;
    }

    public void setMerchantId(String merchantId) {
        this.bizContent.merchantId = merchantId;
    }

    public String getOutTradeNo() {
        return this.bizContent.outTradeNo;
    }

    public void setOutTradeNo(String outTradeNo) {
        this.bizContent.outTradeNo = outTradeNo;
    }

    public String getTradeNo() {
        return this.bizContent.tradeNo;
    }

    public void setTradeNo(String tradeNo) {
        this.bizContent.tradeNo = tradeNo;
    }

    public String getOutRequestNo() {
        return this.bizContent.outRequestNo;
    }

    public void setOutRequestNo(String outRequestNo) {
        this.bizContent.outRequestNo = outRequestNo;
    }

    public String getExternalInfo() {
        return this.bizContent.externalInfo;
    }

    public void setExternalInfo(String externalInfo) {
        this.bizContent.externalInfo = externalInfo;
    }

    public Map<String, String> buildParaMap() {
        super.setBizContent(bizContent.buildBizContentStr());

        if (StringUtils.isNotBlank(acquirerId)) {
            paraMap.put("acquirer_id", acquirerId);
        }
        if (StringUtils.isNotBlank(signType)) {
            paraMap.put("sign_type", signType);
        }
        if (StringUtils.isNotBlank(timestamp)) {
            paraMap.put("timestamp", timestamp);
        }
        if (StringUtils.isNotBlank(appAuthToken)) {
            paraMap.put("app_auth_token", appAuthToken);
        }
        if (StringUtils.isNotBlank(walletType)) {
            paraMap.put("wallet_type", walletType);
        }
        if (StringUtils.isNotBlank(super.bizContent)) {
            paraMap.put("biz_content", super.bizContent);
        }

        return paraMap;

    }

    /** 业务参数 */
    private static class BizContent extends BaseModel {

        /**  */
        private static final long serialVersionUID = 5574479268178892797L;

        // (必填)商户标识号
        @JSONField(name = "merchant_id")
        private String            merchantId;

        // (特殊)订单支付时传入的商户订单号,和支付宝交易号不能同时为空。
        @JSONField(name = "out_trade_no")
        private String            outTradeNo;

        // (特殊)支付宝交易号，和商户订单号不能同时为空
        @JSONField(name = "trade_no")
        private String            tradeNo;

        // (必填)请求退款接口时，传入的退款请求号
        @JSONField(name = "out_request_no")
        private String            outRequestNo;

        // (可选)扩展信息
        @JSONField(name = "external_info")
        private String            externalInfo;

        public String buildBizContentStr() {

            Map<String, Object> bizParaMap = new TreeMap<String, Object>();

            if (StringUtils.isNotBlank(merchantId)) {
                bizParaMap.put("merchant_id", merchantId);
            }
            if (StringUtils.isNotBlank(outTradeNo)) {
                bizParaMap.put("out_trade_no", outTradeNo);
            }
            if (StringUtils.isNotBlank(tradeNo)) {
                bizParaMap.put("trade_no", tradeNo);
            }
            if (StringUtils.isNotBlank(externalInfo)) {
                bizParaMap.put("external_info", externalInfo);
            }
            if (StringUtils.isNotBlank(outRequestNo)) {
                bizParaMap.put("out_request_no", outRequestNo);
            }

            return JSON.toJSONString(bizParaMap);
        }

    }
}
